<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Paginator;
use think\Cache;
use think\DB;

class Index extends BaseController
{
    
    public function index()
    {
        //如果没有登录过跳转到登录页
        $this->checkIndex();
        $usename = Session::get('userinfo');
        $this->assign('uname',$usename);
        $userid = Session::get('userid'); 
        //所有书籍
        $book = db('books')->select();
        //书籍是否已被本人申请
        $borrowstates = db('borrow')->where('states',3)->where('uid',$userid)->select();
        // dump($borrowstates);die;
        foreach($book as $c => $d){
            $book[$c]['type'] = 0;
            foreach($borrowstates as $a => $b){
                if($b['bid']==$d['id']){
                    $book[$c]['type'] = 1;
                }
            }
        }
        // dump($book);die;
        $this->assign('books',$book);
        return view();
    }

    /**
     * 登录页面
     */
    public function login()
    {
        $rollstr = '';
        $nowtime = time();
        $rollcontent = db('horn')->where('isScroll',1)->where('isdelete',0)->order('id','desc')->select();
        if ($rollcontent) {
            foreach ($rollcontent as $key => $value) {
                if (empty($value['expectStoptime'])) {
                    $update = db('horn')->where('isScroll',1)->where('expectStoptime',$value['expectStoptime'])->where('isdelete',0)->setField('isScroll',0);
                }else if($value['expectStoptime']<$nowtime || $value['expectStoptime']==$nowtime){
                    $update = db('horn')->where('isScroll',1)->where('expectStoptime',$value['expectStoptime'])->where('isdelete',0)->setField('isScroll',0);
                }else{
                    $roll = $rollcontent[$key]['content'];
                    $rollstr = $rollstr.'　　'.$roll;
                }
            }
            if ($rollstr!='') {
                $rollstr = substr($rollstr,6);
            }
        }
        // dump($rollstr);die;
        $this->assign('rollinfo',$rollstr);
        return view();
    }

    /**
     * 处理登陆
     */
    public function dologin(){
        $datas = input('post.');
        // return $datas;
        if ($datas['name'] && $datas['pwd']) {
            $data['username'] = $datas['name'];
            $data['password'] = $datas['pwd'];
        }
        $result = db('user')->where('account',$data['username'])->find();
        // return $result;
        if ($result) {
            if ($result['ustates']==1) {
                if ($result['pass'] == md5($data['password'])) {
                    Session::set('userinfo', $result['name']);
                    Session::set('account', $result['account']);
                    Session::set('userid', $result['id']);
                    Session::set('type', $result['type']);
                    $res = $result['type'];
                    return $res;
                }else{
                    return array('msg'=>'密码错误','status'=>'2');
                }
            }else{
                return array('msg'=>'该用户已停用','status'=>'3');
            }
            
        }else{
            return array('msg'=>'不存在该用户','status'=>'4');
        }
    }

    /**
     * 退出登录
     */
    public function logout(){
        Session::clear();
        $this->redirect('/', 302);
    }

    /**
     * 搜索书籍
     */
    public function search(){
        $data = input('post.');
        // dump($data['bname']);die;
        $bnames = $data['bname'];
        $booker = db('books')->where('bname','like','%'.$bnames.'%')->select();
        // dump($booker);die;
        if($booker){
            $res = 1;
        }else{
            $res = 0;
        }
        return $res;
    }
}
